/// @tags: Matrix
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

namespace BlueQuantum {

typedef long long ll;

int const N = 1e2 + 5;

int n, m, k;
char s[N];

struct Matrix;

struct Vector {
  int a[N];

  Vector() { memset(a, 0, sizeof(a)); }

  inline int &operator[](int index) { return a[index]; }
  inline int const &operator[](int index) const { return a[index]; }

  inline Vector operator*(Matrix const &rhs) const;
};

struct Matrix {
  Vector a[N];

  Vector &operator[](int index) { return a[index]; }
  Vector const &operator[](int index) const { return a[index]; }

  inline Matrix operator+(Matrix const &rhs) const;
  inline Matrix operator*(Matrix const &rhs) const;
} I, a, sume, exp;

inline Vector Vector::operator*(Matrix const &rhs) const {
  Vector res;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      if ((res[i] += (ll)a[j] * rhs[j][i] % m) >= m) res[i] -= m;
  return res;
}

inline Matrix Matrix::operator*(Matrix const &rhs) const {
  Matrix res;
  for (int i = 0; i < n; ++i) res[i] = a[i] * rhs;
  return res;
}

inline ostream &operator<<(ostream &os, Vector const &rhs) {
  for (int i = 0; i < n; ++i) os << rhs.a[i] << ' ';
  os << '\n';
  return os;
}

inline ostream &operator<<(ostream &os, Matrix const &rhs) {
  for (int i = 0; i < n; ++i) os << rhs[i];
  os << '\n';
  return os;
}

inline Matrix Matrix::operator+(Matrix const &rhs) const {
  Matrix res;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      if ((res[i][j] = a[i][j] + rhs[i][j]) >= m) res[i][j] -= m;
  return res;
}

void solve(int k) {
  if (k == 1) return sume = exp = a, void();
  solve(k >> 1);
  sume = sume * (I + exp);
  exp = exp * exp;
  if (k & 1) {
    exp = exp * a;
    sume = sume + exp;
  }
}

inline int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    for (int j = 0; j < n; ++j) a[i][j] = (s[j] == 'Y');
  }
  cin >> k >> m;
  for (int i = 0; i < n; ++i) I[i][i] = 1;
  solve(k - 1);
  int ans = 0;
  for (int i = 0; i < n; ++i)
    if ((ans += sume[i][i]) >= m) ans -= m;
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("迂回.in", "r", stdin);
  freopen("迂回.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
